package com.example.rabc.service.impl;

import com.example.rabc.entity.Permission;
import com.example.rabc.service.PermissionComponent;

import java.util.ArrayList;
import java.util.List;

/**
 * 叶子权限节点 - 组合模式中的叶子节点
 * 代表具体的权限项，没有子节点
 */
public class LeafPermission implements PermissionComponent {
    
    private Permission permission;
    private PermissionComponent parent;
    
    public LeafPermission(Permission permission) {
        this.permission = permission;
    }
    
    @Override
    public Long getId() {
        return permission.getId();
    }
    
    @Override
    public String getPermissionCode() {
        return permission.getPermissionCode();
    }
    
    @Override
    public String getPermissionName() {
        return permission.getPermissionName();
    }
    
    @Override
    public boolean hasPermission(String permissionCode) {
        return this.permission.getPermissionCode().equals(permissionCode);
    }
    
    @Override
    public void add(PermissionComponent component) {
        // 叶子节点不能添加子节点，可以抛出异常或忽略
        throw new UnsupportedOperationException("叶子权限节点不能添加子节点");
    }
    
    @Override
    public void remove(PermissionComponent component) {
        // 叶子节点没有子节点可移除
        throw new UnsupportedOperationException("叶子权限节点没有子节点可移除");
    }
    
    @Override
    public List<PermissionComponent> getChildren() {
        // 叶子节点没有子节点
        return new ArrayList<>();
    }
    
    @Override
    public PermissionComponent getParent() {
        return parent;
    }
    
    @Override
    public void setParent(PermissionComponent parent) {
        this.parent = parent;
    }
    
    @Override
    public Permission getPermission() {
        return permission;
    }
}